// pages/register/register.js
const navBarHeight = wx.getSystemInfoSync().statusBarHeight + 44;
const request = require('../../util/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight:navBarHeight,
    username:"",
    password:"",
    phone:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  bindKeyInput(e){
    let tmpStr = e.detail.value
    if(e.currentTarget.dataset.variable === 'username'){
      this.setData({
        username:tmpStr
      })
    }else if(e.currentTarget.dataset.variable === 'password'){
      this.setData({
        password:tmpStr
      })
    }else if(e.currentTarget.dataset.variable === 'phone'){
      this.setData({
        phone:tmpStr
      })
    }
  },

  //注册
  goLogin(){
    var that = this
    let phoneOk = that.isPhoneNumber(that.data.phone)
    if(that.data.username.length === 0){
      wx.showToast({
        title: '请输入用户名',
        icon:'error'
      })
      return
    }else if(that.data.password.length === 0){
      wx.showToast({
        title: '请输入密码',
        icon:'error'
      })
      return
    }else if(phoneOk === false){
      wx.showToast({
        title: '手机号错误',
        icon:'error'
      })
      return
    }
    let data = {
      username:that.data.username,
      password:that.data.password,
      phone:that.data.phone,
    }
    request.requestAjax('register',data, "加载中", function(res) {
      if (res.status == 200) {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/index/index'
          })
        }, 1000)
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
        setTimeout(() => {
          console.log('aaaaaaaaaaaa')
          // wx.navigateTo({
          //   url: '/pages/index/index'
          // })
          wx.switchTab({ url: '/pages/index/index' })
        }, 1000)
      }
    }, function(res) {
        wx.showToast({
            title: '请求失败',
            icon: "none"
        })
    });
  },
  isPhoneNumber(phone) {
    return /^1[3-9]\d{9}$/.test(phone);
  }
})